/**
 * 
 */
package com.rong.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rong.ORM.ZynCar;
import com.rong.ORM.ZynUser;
import com.rong.hibernate.Const;
import com.rong.DAO.ZynCarDAO;

/**
 * @author wafech
 *
 */
public class CarAction extends ActionSupport {
	ZynCar car=new ZynCar();
	ZynCarDAO carDao=new ZynCarDAO();
	private List<ZynCar> list;
	private String saveFlag="";
	private int id=-1; // the car id indatabase.
	
	private String query_carnum="";
	private int query_blongto=-1;
	private String query_driver="";
	private double query_cub=0;
	private double query_oil=0;
	private String query_big="";
	
	private int rowsPerPage =25;// 每页显示几条
	private int page = 1; // 默认当前页
	private int totalPage;// 总共多少页
	private int planNum;// 总过多少条
	    /**
	 * @return the list
	 */
	public List<ZynCar> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ZynCar> list) {
		this.list = list;
	}

		/**
	 * @return the car
	 */
	public ZynCar getCar() {
		return car;
	}

	/**
	 * @param car the car to set
	 */
	public void setCar(ZynCar car) {
		this.car = car;
	}
	
	
		/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}

	
	/**
		 * @return the id
		 */
		public int getId() {
			return id;
		}

		/**
		 * @param id the id to set
		 */
		public void setId(int id) {
			this.id = id;
		}

		
		
		
	/**
		 * @return the query_carnum
		 */
		public String getQuery_carnum() {
			return query_carnum;
		}

		/**
		 * @param query_carnum the query_carnum to set
		 */
		public void setQuery_carnum(String query_carnum) {
			this.query_carnum = query_carnum;
		}

	

		/**
		 * @return the query_blongto
		 */
		public int getQuery_blongto() {
			return query_blongto;
		}

		/**
		 * @param query_blongto the query_blongto to set
		 */
		public void setQuery_blongto(int query_blongto) {
			this.query_blongto = query_blongto;
		}

		/**
		 * @return the query_driver
		 */
		public String getQuery_driver() {
			return query_driver;
		}

		/**
		 * @param query_driver the query_driver to set
		 */
		public void setQuery_driver(String query_driver) {
			this.query_driver = query_driver;
		}

		/**
		 * @return the query_cub
		 */
		public double getQuery_cub() {
			return query_cub;
		}

		/**
		 * @param query_cub the query_cub to set
		 */
		public void setQuery_cub(double query_cub) {
			this.query_cub = query_cub;
		}

		/**
		 * @return the query_oil
		 */
		public double getQuery_oil() {
			return query_oil;
		}

		/**
		 * @param query_oil the query_oil to set
		 */
		public void setQuery_oil(double query_oil) {
			this.query_oil = query_oil;
		}

		/**
		 * @return the query_big
		 */
		public String getQuery_big() {
			return query_big;
		}

		/**
		 * @param query_big the query_big to set
		 */
		public void setQuery_big(String query_big) {
			this.query_big = query_big;
		}
		

	/**
		 * @return the rowsPerPage
		 */
		public int getRowsPerPage() {
			return rowsPerPage;
		}

		/**
		 * @param rowsPerPage the rowsPerPage to set
		 */
		public void setRowsPerPage(int rowsPerPage) {
			this.rowsPerPage = rowsPerPage;
		}

		/**
		 * @return the page
		 */
		public int getPage() {
			return page;
		}

		/**
		 * @param page the page to set
		 */
		public void setPage(int page) {
			this.page = page;
		}

		/**
		 * @return the totalPage
		 */
		public int getTotalPage() {
			return totalPage;
		}

		/**
		 * @param totalPage the totalPage to set
		 */
		public void setTotalPage(int totalPage) {
			this.totalPage = totalPage;
		}

		/**
		 * @return the planNum
		 */
		public int getPlanNum() {
			return planNum;
		}

		/**
		 * @param planNum the planNum to set
		 */
		public void setPlanNum(int planNum) {
			this.planNum = planNum;
		}

	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}

		@Override
	    public String execute() throws Exception {
	        this.setSaveFlag("");
			carDao.save(this.car);        //保存注册的car对象
			 this.setSaveFlag("保存成功..");
	        return SUCCESS;
	    }
		 public String findall() throws Exception {
		        
			 ActionContext actionContext = ActionContext.getContext();         
	         Map session = actionContext.getSession(); 
	         ZynUser loguser=(ZynUser) session.get("loginUser");    // the user session
	         if(loguser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有
	        	 this.setList(carDao.findAll());        //所有
			 
	         }else{
	        	 this.setList(carDao.findByCompany(String.valueOf(loguser.getComId())));        //所在分公司车辆
	        	 
	         }
		        return "success_list";
		    }
		 public String findbyid() throws Exception {
		        
			this.setCar(carDao.findById(this.getId()));// find the car by id			
		        return "s_find_one";
		       
		    }
		 public String edit() throws Exception {
		      			
			   carDao.merge(this.getCar());// edit the car by id			
			    return "s_edit_one";
			    }
		 public String delete() throws Exception {
		        
				carDao.delete(carDao.findById(this.getId()));// delete the car by id			
			    return "s_delete_one";
			    }
		 
		 public String toGetCarList() throws Exception {
			    
				this.setList(carDao.findCarQueryByPage(page, rowsPerPage,this.getQuery_carnum(),this.getQuery_driver(),this.getQuery_blongto(),this.getQuery_cub(),this.getQuery_oil(),this.getQuery_big()));       
		        this.setPlanNum(carDao.carTatolRows(this.getQuery_carnum(),this.getQuery_driver(),this.getQuery_blongto(),this.getQuery_cub(),this.getQuery_oil(),this.getQuery_big()).size());
		        this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);     
			        return "success_list";
			    }
			 
	}
